<template>
  <div class="main">
    <el-card class="search-box">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="分类名称" prop="cateName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="请输入分类名称"
            clearable
            @keyup.enter.native="getCteList"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="time">
          <el-date-picker
          v-model="value"
            type="daterange"
            format="yyyy-MM-dd HH:mm:ss" 
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="selectTime"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getCteList"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top:10px;">
      <el-button style="margin-bottom:20px" icon="el-icon-plus" size="mini" type="primary" @click="addCate(1,0)"
      >添加分类</el-button>
      <tree-table
      :data="cateList"
      :columns="columns"
      :selection-type="false"
      :expand-type="false"
      show-index
      index-text="序列"
      border
    >
      <template v-slot:isok="scope">
        <i
          class="el-icon-success"
          style="color: lightgreen"
          v-if="scope.row.isShow === true"
        ></i>
        <i class="el-icon-error" style="color: red" v-else></i>
        {{scope.row.isShow?'显示':'不显示'}}
      </template>
      <template v-slot:operate="scope">
        <el-button size="mini" @click="addCate(1,scope.row.id)" type="warning" v-if="scope.row.parentId == 0" >添加下级</el-button>
        <el-button :class="scope.row.parentId !==0?'rightM':''" size="mini" @click="editIshow(scope.row.id,scope.row.isShow)" :type="scope.row.isShow?'info':'success'">{{scope.row.isShow?'隐藏':'显示'}}</el-button>
        <el-button size="mini" @click="editCate(0,scope.row)" type="primary">编辑</el-button>
        <el-button size="mini" @click="deletdCate(scope.row.id)" :disabled="scope.row.goodsCount>0" type="danger">删除</el-button>
      </template>
    </tree-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNumber" :limit.sync="queryParams.pageSize" @pagination="getCteList" />
    </el-card>
    <el-dialog
      :title="titleType==1?'添加分类':'编辑分类'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="ruleForm.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-radio-group v-model="ruleForm.isShow">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">不显示</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类图片" prop="categoryImage">
          <image-upload :limit="1" v-model="ruleForm.categoryImage" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
    </el-form>
      <!-- <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span> -->
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisibleClose"
      width="30%"
      :before-close="handleDeleteClose">
      <span>确定进行删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleClose = false">取 消</el-button>
        <el-button type="primary" @click="submitDelete()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { goodsCate,addGoodsCate,editGoodsCate,editGoodsIsShowCate,deleteGoodsCate } from '@/api/goods'
export default {
  name: '',
  components: { Pagination },
  props: {},
  data() {
    return {
      queryParams:{
        categoryName:'',
        pageSize:10,
        pageNumber:1,
        startTime:'',
        endTime:'',
      },
      total:0,
      cateList:[],
      columns:[
        {
          label:'分类名称',
          prop:'categoryName'
        },
        {
          label:'商品数量',
          prop:'goodsCount'
        },
        {
          label:'是否显示',
          type:'template',
          template: 'isok'
        },
        {
          label:'创建时间',
          prop:'createTime'
        },
        {
          label:'操作',
          type:'template',
          template:'operate'
        },
      ],
      dialogVisible:false,
      dialogVisibleClose:false,
      titleType:true,
      ruleForm:{
        categoryName:'',
        isShow:1,
        parentId: null,
        categoryImage:''
      },
      deleteId:null,
      rules: {
        categoryName: [
            { required: true, message: '请输入名称', trigger: 'blur' },
          ],
          categoryImage: [
            { required: true, message: '请上传图片', trigger: 'blur' },
          ],
      },
      value:''
    }
  },
  watch: {},
  computed: {},
  methods: {
    resetQuery(){
      this.queryParams={
        categoryName:'',
        pageSize:10,
        pageNumber:1,
        startTime:'',
        endTime:'',

      }
      this.getCteList()
    },
    selectTime(val){
      this.queryParams.startTime = val[0]+''
      this.queryParams.endTime = val[1]+''
    },
    getCteList(){
      goodsCate(this.queryParams).then(res=>{
        this.cateList =  JSON.parse(JSON.stringify(res.data.list).replace(/childrenCategoryList/g, 'children'))
        this.total = res.data.total
      })
    },
    submitForm(formName) {
      console.log(this.ruleForm,'ruleFormruleFormruleForm')
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.titleType){
              this.getAddGoodsCate()
            }else{
              this.getEditCate( )
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },
    handleClose(){
      this.dialogVisible = false
    },
    handleDeleteClose(){
      this.dialogVisibleClose = false
    },
    addCate(type,parentId){
      this.dialogVisible = true
      this.ruleForm.parentId = parentId
      this.titleType  = type
    },
    editCate(type,data){
      this.dialogVisible = true
      this.titleType  = type
      this.ruleForm = {
        categoryName:data.categoryName,
        categoryImage:data.categoryImage,
        isShow:data.isShow?1:0,
        parentId: null,
        id:data.id
      }
      this.ruleForm.id = data.id
      
    },
    getAddGoodsCate(){
      addGoodsCate(this.ruleForm).then(res=>{
        if(res.code == 200){
          this.dialogVisible = false
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getCteList()
          this.ruleForm = {
            categoryName:'',
            isShow:1,
            parentId: null,
          }
        }
      })
    },
    getEditCate(){
      editGoodsCate(this.ruleForm).then(res=>{
        if(res.code == 200){
          this.dialogVisible = false
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getCteList()
          this.ruleForm = {
            categoryName:'',
            isShow:1,
            parentId: null,
            id: null,
          }
        }
      })
    },
    editIshow(id,type){
      let state = type?'hide':'show'
      editGoodsIsShowCate(state,id).then(res=>{
        this.$message({
          message: res.message,
          type: 'success'
        });
        this.getCteList()
      })
    },
    deletdCate(id){
      this.dialogVisibleClose = true
      this.deleteId = id
    },
    submitDelete(){
      deleteGoodsCate(this.deleteId).then(res=>{
        this.$message({
          message: res.message,
          type: 'success'
        });
        this.dialogVisibleClose = false
        this.getCteList()
      })
    },
    resetForm(){
      this.dialogVisible=false,
      this.ruleForm = {
            categoryName:'',
            isShow:1,
            parentId: null,
            id: null,
          }
    }
  },
  created() {
    this.getCteList()
  },
  mounted() {}
}
</script>

<style lang="scss" scoped>
  .el-form-item{
    margin-right: 20px;
  }
  .rightM{
    margin-left: 90px;
  }
  ::v-deep  .el-upload--picture-card{
    width: 98px;
    height: 98px;
    line-height: 98px;
  }
</style>



